Did
you ever notice that when you're filling in a web-based form in Mobile
Safari, a small row of additional buttons appears above the keyboard,
containing buttons labeled Previous, Next, and so on? iOS 3.2 gives us
a way to do that sort of thing, too, using the new inputAccessoryView property of UITextView and UITextField. You just put anything you like into a UIView (buttons, labels, sliders ... you name it), and pass that view off to the text-input object in question.
As if that weren't enough,
starting with OS 3.2 we can now replace the entire keyboard! The idea
is similar to that for the accessory view. UIView has a new inputView property that you can set, using any UIView you like.
1. Adding a Keyboard Button in Dudel
To demonstrate how to extend the keyboard, we're going to add something that has been missing from the FileRenameViewController in Dudel: the ability to cancel the renaming operation. We'll do this by adding a Cancel button to the keyboard.
Open FileRenameViewController.m, and add the following method to set up the input accessory view:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *inputAccessoryView = [[UIView alloc] initWithFrame:
CGRectMake(0.0, 0.0, 768.0, 77.0)];
inputAccessoryView.backgroundColor = [UIColor darkGrayColor];
UIButton *cancelButton = [UIButton buttonWithType:
UIButtonTypeRoundedRect];
cancelButton.frame = CGRectMake(20.0, 20.0, 100.0, 37.0);
[cancelButton setTitle: @"Cancel" forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor blackColor] forState:
UIControlStateNormal];
[cancelButton addTarget:self action:@selector(cancel:)
forControlEvents:UIControlEventTouchUpInside];
[inputAccessoryView addSubview:cancelButton];
textField.inputAccessoryView = inputAccessoryView;
}
Also add the method that actually does the canceling:
- (void)cancel:(id)sender {
[delegate fileRenameViewController:self didRename:originalFilename
to:originalFilename];
}
Now build and run your app, bring up the file renaming view, and you'll see something like Figure 1.